﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using DevExpress.Utils.Design;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;
using System.ComponentModel;
using System.Drawing;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Helper;
using XCI.Sys;
using XCI.Sys.Filter;
using XCI.Sys.Model;
using XCI.Sys.Service;

namespace XCI.Windows
{
    /// <summary>
    /// 角色下拉控件
    /// </summary>
    public class RoleComboBoxEdit : ComboBoxEditx, IAsyncBindingData
    {
        static RoleComboBoxEdit()
        {
            RepositoryItemRoleComboBoxEdit.RegisterRoleComboBoxEdit();
        }

        [Category("XCI-Properties")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemRoleComboBoxEdit Properties => base.Properties as RepositoryItemRoleComboBoxEdit;

        public override string EditorTypeName => RepositoryItemRoleComboBoxEdit.CustomEditName;

        [DefaultValue("Name")]
        [SmartTagProperty("DisplayMember", "XCI", SmartTagActionType.RefreshBoundsAfterExecute)]
        public override string DisplayMember => "Name";

        [DefaultValue("Value")]
        [SmartTagProperty("ValueMember", "XCI", SmartTagActionType.RefreshBoundsAfterExecute)]
        public override string ValueMember => "Id";

        /// <summary>
        /// 是否显示全部项
        /// </summary>
        [DefaultValue(true)]
        [Category("XCI")]
        [Description("是否显示全部项")]
        public virtual bool AllItemVisible { get; set; } = true;

        /// <summary>
        /// 全部项文本
        /// </summary>
        [DefaultValue("全部分类")]
        [Category("XCI")]
        [Description("全部项文本")]
        public virtual string AllItemCaption { get; set; } = "全部";

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (WinFormHelper.IsDesignMode()) return;
        }

        /// <summary>
        /// 数据异步绑定
        /// </summary>
        /// <returns>等待的任务</returns>
        public async Task BindingDataAsync()
        {
            var deptId = SysApp.CurrentUser.IsAdmin ? null : SysApp.CurrentUser.DeptId;
            var roleList = await SysRoleService.Instance.SelectListAsync(new SysRoleFilter
            {
                Status = true,
                EnableDeptScope = true,
                DeptId = deptId
            });
            if (AllItemVisible)
            {
                roleList.Insert(0, new SysRole
                {
                    Id = string.Empty,
                    Code = string.Empty,
                    Name = AllItemCaption,
                    Status = false
                });
            }
            DataSource = roleList;
        }
    }

    [UserRepositoryItem("RegisterRoleComboBoxEdit")]
    public class RepositoryItemRoleComboBoxEdit : RepositoryItemComboBoxEditx
    {
        static RepositoryItemRoleComboBoxEdit()
        {
            RegisterRoleComboBoxEdit();
        }

        public new const string CustomEditName = "RoleComboBoxEdit";

        public RepositoryItemRoleComboBoxEdit()
        {
        }

        public override string EditorTypeName => CustomEditName;

        public static void RegisterRoleComboBoxEdit()
        {
            Image img = null;
            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName,
                typeof(RoleComboBoxEdit), typeof(RepositoryItemRoleComboBoxEdit),
                typeof(ComboBoxEditxViewInfo), new ComboBoxEditxPainter(), true, img));
        }

        public override void Assign(RepositoryItem item)
        {
            BeginUpdate();
            try
            {
                base.Assign(item);
                if (!(item is RepositoryItemRoleComboBoxEdit source)) return;
                //
            }
            finally
            {
                EndUpdate();
            }
        }
    }
}